package com.parking.carsystem.parkingchargesystem.utils;

import android.annotation.SuppressLint;
import android.content.Context;

import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 作者： 小明哥哥
 * 创建时间：2019/1/2
 * 描述： 微信支付工具类
 */
public class WXPayUtils {

    private final WXPayBuilder builder;

    private WXPayUtils(WXPayBuilder builder) {
        this.builder = builder;
    }


    /**
     * 微信支付业务
     */
    @SuppressLint("CheckResult")
    public void pay(Context context, String appId) {
        final IWXAPI api = WXAPIFactory.createWXAPI(context, appId);
        new Thread(new Runnable() {
            @Override
            public void run() {
                PayReq request = new PayReq(); //调起微信APP的对象
                request.appId = builder.getAppId();
                request.partnerId = builder.getPartnerId();
                request.prepayId = builder.getPrepayId();
                request.packageValue = builder.getPackageValue();
                request.nonceStr = builder.getNonceStr();
                request.timeStamp = builder.getTimeStamp();
                request.sign = builder.getSign();
                api.sendReq(request);//发送调起微信的请求
            }
        }).start();
    }


    public static class WXPayBuilder {
        private String appId;
        private String partnerId;
        private String prepayId;
        private String packageValue;
        private String nonceStr;
        private String timeStamp;
        private String sign;

        public WXPayBuilder(JSONObject json) throws JSONException {
            this.appId = json.getString("appid");
            this.partnerId = json.getString("partnerid");
            this.prepayId = json.getString("prepayid");
            this.packageValue = json.getString("package");
            this.nonceStr = json.getString("noncestr");
            this.timeStamp = json.getString("timestamp");
            this.sign = json.getString("sign");
        }

        public WXPayUtils build() {
            return new WXPayUtils(this);
        }

        public String getAppId() {
            return appId;
        }

        public WXPayBuilder setAppId(String appId) {
            this.appId = appId;
            return this;
        }

        public String getPartnerId() {
            return partnerId;
        }

        public WXPayBuilder setPartnerId(String partnerId) {
            this.partnerId = partnerId;
            return this;
        }

        public String getPrepayId() {
            return prepayId;
        }

        public WXPayBuilder setPrepayId(String prepayId) {
            this.prepayId = prepayId;
            return this;
        }

        public String getPackageValue() {
            return packageValue;
        }

        public WXPayBuilder setPackageValue(String packageValue) {
            this.packageValue = packageValue;
            return this;
        }

        public String getNonceStr() {
            return nonceStr;
        }

        public WXPayBuilder setNonceStr(String nonceStr) {
            this.nonceStr = nonceStr;
            return this;
        }

        public String getTimeStamp() {
            return timeStamp;
        }

        public WXPayBuilder setTimeStamp(String timeStamp) {
            this.timeStamp = timeStamp;
            return this;
        }

        public String getSign() {
            return sign;
        }

        public WXPayBuilder setSign(String sign) {
            this.sign = sign;
            return this;
        }

        @Override
        public String toString() {
            return "WXPayBuilder{" +
                    "appId='" + appId + '\'' +
                    ", partnerId='" + partnerId + '\'' +
                    ", prepayId='" + prepayId + '\'' +
                    ", packageValue='" + packageValue + '\'' +
                    ", nonceStr='" + nonceStr + '\'' +
                    ", timeStamp='" + timeStamp + '\'' +
                    ", sign='" + sign + '\'' +
                    '}';
        }
    }
}
